package com.hmdp.service;

import com.hmdp.entity.MessageRecord;
import com.hmdp.mapper.MessageRecordMapper;
import com.hmdp.entity.MessageStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class MessageRecordService {
    
    @Autowired
    private MessageRecordMapper messageRecordMapper;
    
    @Transactional
    public void save(MessageRecord messageRecord) {
        messageRecordMapper.insert(messageRecord);
    }

    @Transactional
    public void updateStatus(String messageId, MessageStatus status) {
        messageRecordMapper.updateStatus(messageId, status.getCode());
    }

    @Transactional
    public void updateForRetry(String messageId, int retryCount, Date nextRetryTime) {
        // 修改为尝试中
        messageRecordMapper.updateForRetry(messageId, 2, retryCount, nextRetryTime);
    }
    
    public List<MessageRecord> queryPendingMessages(int limit) {
        return messageRecordMapper.queryPendingMessages(limit);
    }

    //更新为失败
    public void updateFail(String messageId, int retryCount, Date nextRetryTime) {
        messageRecordMapper.updateForRetry(messageId, 3, retryCount, nextRetryTime);
    }
}